package com.example.demo.controller;

import com.example.demo.dto.ApiResponse;
import com.example.demo.entity.Lab;
import com.example.demo.entity.TimeSlot;
import com.example.demo.service.LabService;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/labs")
@RequiredArgsConstructor
public class LabController {

    private final LabService labService;

    @GetMapping("/available")
    public ApiResponse<?> getAvailableLabs(
            @RequestParam(required = false) String date,
            @RequestParam(required = false) String timeSlot) {
        return labService.getAvailableLabs(date, timeSlot);
    }

    @GetMapping("/{id}")
    public ApiResponse<?> getLabById(@PathVariable Long id) {
        return labService.getLabById(id);
    }

    @GetMapping
    public ApiResponse<?> getAllLabs() {
        return labService.getAllLabs();
    }

    @GetMapping("/category/{categoryId}")
    public ApiResponse<?> getLabsByCategory(@PathVariable Long categoryId) {
        return labService.getLabsByCategory(categoryId);
    }

    @PostMapping
    @PreAuthorize("hasRole('TEACHER')")
    public ApiResponse<?> createLab(@RequestBody Lab lab) {
        return labService.createLab(lab);
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasRole('TEACHER')")
    public ApiResponse<?> updateLab(@PathVariable Long id, @RequestBody Lab lab) {
        return labService.updateLab(id, lab);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('TEACHER')")
    public ApiResponse<?> deleteLab(@PathVariable Long id) {
        return labService.deleteLab(id);
    }

    @PostMapping("/{id}/images")
    public ApiResponse<?> addImage(@PathVariable Long id, @RequestParam String imageUrl) {
        return ApiResponse.success("图片添加成功", labService.addImage(id, imageUrl));
    }

    @DeleteMapping("/{id}/images")
    public ApiResponse<?> removeImage(@PathVariable Long id, @RequestParam String imageUrl) {
        return ApiResponse.success("图片删除成功", labService.removeImage(id, imageUrl));
    }

    @PostMapping("/{labId}/time-slots")
    public ApiResponse<?> addTimeSlot(@PathVariable Long labId, @RequestBody TimeSlot timeSlot) {
        return labService.addTimeSlot(labId, timeSlot);
    }

    @DeleteMapping("/{labId}/time-slots/{timeSlotId}")
    public ApiResponse<?> removeTimeSlot(@PathVariable Long labId, @PathVariable Long timeSlotId) {
        return labService.removeTimeSlot(labId, timeSlotId);
    }

    @PutMapping("/{labId}/time-slots/{timeSlotId}")
    public ApiResponse<?> updateTimeSlot(@PathVariable Long labId, @PathVariable Long timeSlotId, @RequestBody TimeSlot timeSlot) {
        return labService.updateTimeSlot(labId, timeSlotId, timeSlot);
    }

    @GetMapping("/{labId}/time-slots")
    public ApiResponse<?> getTimeSlots(@PathVariable Long labId) {
        return labService.getTimeSlots(labId);
    }
} 